package com.hotel.reservation.controller.admin;

import com.hotel.reservation.service.ReserveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.hotel.reservation.po.ReservePo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 0.1
 * @Author: oliver chen
 * @Description:
 * @Date:Create：in 2021/7/25 13:33
 * @Modified By：
 */
@Controller
@RequestMapping("/admin/reserve")
public class AdminReserveController {
    @Autowired
    private ReserveService reserveService;

    @RequestMapping("/selectAll")
    public String selectAll(Model model,Integer pageCur){
        List<ReservePo>  reservePos= reserveService.selectAll();
        int temp =reservePos.size();
        model.addAttribute("totalCount",temp);
        int totalPage=0;
        if (temp==0){
            totalPage=0;//总页数
        }else {
            //返回大于或等于指定表达式的最小整数
            totalPage=(int)Math.ceil((double) temp/10);
        }
        if (pageCur==null){
            pageCur=1;
        }
        if ((pageCur-1)*10>temp){
            pageCur=pageCur-1;
        }
        //分页查询
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("startIndex",(pageCur-1)*10);//起始位置
        map.put("perPageSize",10);//每页10个
        reservePos = reserveService.selectAllReserveByPage(map);
        model.addAttribute("orderList",reservePos);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("pageCur",pageCur);
        return "admin/orderManager";
    }

    @RequestMapping("/selectByRoomId")
    public List<ReservePo> selectByRoomId(String roomId){return reserveService.selectByRoomId(roomId);}


    @RequestMapping("/deleteReserve")
    public String deleteReserve(int reserveId,Model model){
        Integer flag = reserveService.deleteByRoomId(reserveId);
        if (flag>0) {
            model.addAttribute("message", "删除成功");
        }else {
            model.addAttribute("message", "删除失败");
        }
        return "forward:/admin/reserve/selectAll";
    }

}
